package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.google.common.collect.Maps;
import com.itheima.dao.ItmereportDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ItmereportService.class)
@Transactional
public class ItmereportServiceImpl implements ItmereportService {

    @Autowired
    ItmereportDao itmereportDao;

    @Override
    public List<Map> findsex() {
        Map<String,String> map1 = new HashMap<>();
        Map<String,String> map2 = new HashMap<>();
        map1.put("name","男");
        map2.put("name","女");

        List<Integer> sexlist = itmereportDao.ifsex();

        int m = 0;
        int r = 0;

        for (Integer sex : sexlist) {
            if (sex == 1){
                ++m;
            }else {
                ++r;
            }
        }
        map1.put("value",String.valueOf(m));
        map2.put("value",String.valueOf(r));
        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);

        return list;
    }

    @Override
    public List<Map> findage() {
        List<Integer> ages = itmereportDao.findagecount();


        Map<String,String> maps1 = new HashMap<>();
        Map<String,String> maps2 = new HashMap<>();
        Map<String,String> maps3 = new HashMap<>();
        Map<String,String> maps4 = new HashMap<>();
        int a =0;
        int b =0;
        int c =0;
        int d =0;
        maps1.put("name","0-18");
        maps2.put("name","18-30");
        maps3.put("name","30-45");
        maps4.put("name","45以上");
        for (Integer age : ages) {
            if (age>=0 && age<=18){
                a++;
                maps1.put("value",String.valueOf(a));
            }
            else if (age>18 && age <=30){
                b++;
                maps2.put("value",String.valueOf(b));
            }else if (age>30 && age <=45){
                c++;maps3.put("value",String.valueOf(c));

            }else {
                d++;
                maps4.put("value",String.valueOf(d));
            }
        }


        List<Map> list = new ArrayList<>();
        list.add(maps1);
        list.add(maps2);
        list.add(maps3);
        list.add(maps4);
        return list;
    }


}
